package com.wman.sheep.mvp.base;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.wman.sheep.R;
import com.wman.sheep.mvp.presenter.FragmentPresenter;
import com.wman.sheep.mvp.view.IDelegate;

import java.lang.ref.WeakReference;




/**
 * Created by wman on 17-4-18.
 */
public abstract class BaseFragment<T extends IDelegate> extends FragmentPresenter<T>
        implements View.OnClickListener {

    @Override
    public void fetchData() {

    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 实例化一个UiHandler处理
     */
    protected Handler mUiHandler = new UiHandler(this) {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (getFragmentReference() != null && getFragmentReference().get() != null) {
                try {
                    getFragmentReference().get().handleUiMessage(msg);
                } catch (IllegalStateException e) {
                }
            }
        }
    };

    /**
     * 自定义UiHandler,用于处理其他线程发送的消息
     *
     * @author dane55
     */
    private static class UiHandler extends Handler {
        // 定义RCBaseFragment的弱引用，以保证虚拟机随时可以回收此页面
        private final WeakReference<BaseFragment> mFragmentReference;

        /**
         * 构建自定义的UIHandler，传递当前页面引用(Activity或者BaseFragment)
         *
         * @param activity
         */
        public UiHandler(BaseFragment activity) {
            mFragmentReference = new WeakReference<BaseFragment>(activity);
        }

        /**
         * 获取当前页面(Activity或者BaseFragment)
         *
         * @return
         */
        public WeakReference<BaseFragment> getFragmentReference() {
            return mFragmentReference;
        }
    }

    /**
     * 处理更新UI任务(派生类需要重写此方法，用于处理自身业务)
     *
     * @param msg
     */
    protected void handleUiMessage(Message msg) {
    }

    /**
     * 发送UI更新操作
     *
     * @param msg
     */
    protected void sendUiMessage(Message msg) {
        mUiHandler.sendMessage(msg);
    }

    /**
     * 发送UI更新操作
     *
     * @param what
     */
    public void sendEmptyUiMessage(int what) {
        mUiHandler.sendEmptyMessage(what);
    }

    /**
     * 接收来自BroadcastReceiver数据集
     *
     * @param context
     * @param intent
     */
    public void onReceive(Context context, Intent intent) {

    }

    /**
     * 隐藏软键盘
     */
    protected void hideSoftInput(Context context) {
        InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        // manager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        if (getActivity().getCurrentFocus() != null) {
            manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 显示软键盘
     */
    protected void showSoftInput() {
        InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }

    /**
     * @param it
     * @return void
     * @Title: startAnimationActivity
     * @Description: 开始动画跳转
     */
    public void startAnimationActivity(Intent it) {
        getActivity().startActivity(it);
        getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_of_left);
    }

    /**
     * @param it
     * @param requestCode
     * @return void
     * @Title: startAnimationActivityForResult
     * @Description: 开始动画跳转
     */
    public void startAnimationActivityForResult(Intent it, int requestCode) {
        getActivity().startActivityForResult(it, requestCode);
        getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_of_left);
    }

    /**
     * @return void
     * @Title: finishAnimationActivity
     * @Description: 动画方式结束页面
     */
    public void finishAnimationActivity() {
        getActivity().finish();
        getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_of_right);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUiHandler.removeCallbacksAndMessages(null);
    }
}
